home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / ln / RCS / ln.c,v < prev    next >
Encoding:
Text File  |  1990-01-31  |  5.4 KB  |  289 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     90.01.31.14.48.15;  author jhh;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     88.07.21.14.45.18;  author ouster;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     88.07.21.14.31.05;  author ouster;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @@
  27.  
  28.  
  29. 1.3
  30. log
  31. @made remote links more general
  32. @
  33. text
  34. @/*
  35.  * Copyright (c) 1987 Regents of the University of California.
  36.  * All rights reserved.
  37.  *
  38.  * Redistribution and use in source and binary forms are permitted
  39.  * provided that this notice is preserved and that due credit is given
  40.  * to the University of California at Berkeley. The name of the University
  41.  * may not be used to endorse or promote products derived from this
  42.  * software without specific written prior permission. This software
  43.  * is provided ``as is'' without express or implied warranty.
  44.  */
  45.  
  46. #ifndef lint
  47. char copyright[] =
  48. "@@(#) Copyright (c) 1987 Regents of the University of California.\n\
  49.  All rights reserved.\n";
  50. #endif /* not lint */
  51.  
  52. #ifndef lint
  53. static char sccsid[] = "@@(#)ln.c    4.10 (Berkeley) 11/30/87";
  54. #endif /* not lint */
  55.  
  56. #include <sys/param.h>
  57. #include <sys/stat.h>
  58. #include <stdio.h>
  59. #include <errno.h>
  60.  
  61. #include <sprite.h>
  62. #include <fs.h>
  63. #include <status.h>
  64.  
  65. static int    fflag,                /* undocumented force flag */
  66.         sflag,                /* symbolic, not hard, link */
  67.         rflag,                /* remote link for Sprite */
  68.         (*linkf)();            /* system link call */
  69.  
  70. /*
  71.  * Forward declarations to keep gcc happy:
  72.  */
  73.  
  74. static int linkit(), usage(), remoteLink();
  75.  
  76. main(argc, argv)
  77.     int    argc;
  78.     char    **argv;
  79. {
  80.     extern int    optind;
  81.     struct stat    buf;
  82.     int    ch, exitval, link(), symlink();
  83.     char    *sourcedir;
  84.  
  85.     while ((ch = getopt(argc, argv, "fsr")) != EOF)
  86.         switch((char)ch) {
  87.         case 'f':
  88.             fflag = 1;
  89.             break;
  90.         case 's':
  91.             sflag = 1;
  92.             break;
  93.         case 'r':
  94.             rflag = 1;
  95.             break;
  96.         case '?':
  97.         default:
  98.             usage();
  99.         }
  100.  
  101.     argv += optind;
  102.     argc -= optind;
  103.  
  104.     if (rflag && sflag) {
  105.         usage();
  106.     }
  107.  
  108.     if (sflag) {
  109.         linkf = symlink;
  110.     } else if (rflag) {
  111.         linkf = remoteLink;
  112.     } else {
  113.         linkf = link;
  114.     }
  115.  
  116.     switch(argc) {
  117.     case 0:
  118.         usage();
  119.     case 1:                /* ln target */
  120.         exit(linkit(argv[0], ".", 1));
  121.     case 2:                /* ln target source */
  122.         exit(linkit(argv[0], argv[1], 0));
  123.     default:            /* ln target1 target2 directory */
  124.         sourcedir = argv[argc - 1];
  125.         if (stat(sourcedir, &buf)) {
  126.             perror(sourcedir);
  127.             exit(1);
  128.         }
  129.         if ((buf.st_mode & S_IFMT) != S_IFDIR)
  130.             usage();
  131.         for (exitval = 0; *argv != sourcedir; ++argv)
  132.             exitval |= linkit(*argv, sourcedir, 1);
  133.         exit(exitval);
  134.     }
  135.     /*NOTREACHED*/
  136. }
  137.  
  138. static
  139.  linkit(target, source, isdir)
  140.     char    *target, *source;
  141.     int    isdir;
  142. {
  143.     extern int    errno;
  144.     struct stat    buf;
  145.     char    path[MAXPATHLEN],
  146.         *cp, *rindex(), *strcpy();
  147.  
  148.     if (!sflag && !rflag) {
  149.         /* if target doesn't exist, quit now */
  150.         if (stat(target, &buf)) {
  151.             perror(target);
  152.             return(1);
  153.         }
  154.         /* only symbolic links to directories, unless -f option used */
  155.         if (!fflag && (buf.st_mode & S_IFMT) == S_IFDIR) {
  156.             printf("%s is a directory.\n", target);
  157.             return(1);
  158.         }
  159.     }
  160.  
  161.     /* if the source is a directory, append the target's name */
  162.     if (isdir || !stat(source, &buf) && (buf.st_mode & S_IFMT) == S_IFDIR) {
  163.         if (!(cp = rindex(target, '/')))
  164.             cp = target;
  165.         else
  166.             ++cp;
  167.         (void)sprintf(path, "%s/%s", source, cp);
  168.         source = path;
  169.     }
  170.  
  171.     if ((*linkf)(target, source)) {
  172.         perror(source);
  173.         return(1);
  174.     }
  175.     return(0);
  176. }
  177.  
  178. static
  179. remoteLink(target, source)
  180.     char *target;
  181.     char *source;
  182. {
  183.     struct stat stb;
  184.     ReturnStatus status;
  185.  
  186.     if (target[0] != '/') {
  187.     fprintf(stderr, "Target \"%s\" should be an absolute path\n", target);
  188.     return(1);
  189.     }
  190.     if (lstat(source, &stb) >= 0) {
  191.     /*
  192.      * The link already exists.  Rewrite it if it's a remote link,
  193.      * otherwise complain.
  194.      */
  195.     if ((stb.st_mode&S_IFMT) != S_IFRLNK) {
  196.         fprintf(stderr, "\"%s\" exists\n",source);
  197.         return(1);
  198.     } else if (unlink(source) < 0) {
  199.         perror(target);
  200.         return(1);
  201.     }
  202.     }
  203.     status = Fs_SymLink(target, source, 1);
  204.     if (status != 0) {
  205.     Stat_PrintMsg(status, "Fs_SymLink");
  206.     return(1);
  207.     }
  208.     return(0);
  209. }
  210.  
  211. static
  212. usage()
  213. {
  214.     fputs("usage:\tln [-s | -r] targetname [sourcename]\n\tln [-s | -r] targetname1 targetname2 [... targetnameN] sourcedirectory\n\n", stderr);
  215.     exit(1);
  216. }
  217. @
  218.  
  219.  
  220. 1.2
  221. log
  222. @Add "-r" switch for remote links.
  223. @
  224. text
  225. @d41 1
  226. a41 1
  227. extern int linkit(), usage(), remoteLink();
  228. d71 2
  229. a72 10
  230.     if (rflag) {
  231.         /*
  232.          * Sprite remote link.  A kind of circular symbolic link that
  233.          * indicates to the kernel that a remote domain is being entered.
  234.          * This is used to do dynamic mounting via the prefix table.
  235.          */
  236.         if ((argc != 1) || sflag) {
  237.         usage();
  238.         }
  239.         exit(remoteLink(argv[0]));
  240. d75 7
  241. a81 1
  242.     linkf = sflag ? symlink : link;
  243. d106 1
  244. a106 1
  245. linkit(target, source, isdir)
  246. d115 1
  247. a115 1
  248.     if (!sflag) {
  249. d146 1
  250. a146 1
  251. remoteLink(target)
  252. d148 1
  253. d157 1
  254. a157 1
  255.     if (lstat(target, &stb) >= 0) {
  256. d163 1
  257. a163 1
  258.         fprintf(stderr, "\"%s\" exists\n", target);
  259. d165 1
  260. a165 1
  261.     } else if (unlink(target) < 0) {
  262. d170 1
  263. a170 1
  264.     status = Fs_SymLink(target, target, 1);
  265. d181 1
  266. a181 1
  267.     fputs("usage:\tln [-s] targetname [sourcename]\n\tln [-s] targetname1 targetname2 [... targetnameN] sourcedirectory\n\tln -r prefix\n", stderr);
  268. @
  269.  
  270.  
  271. 1.1
  272. log
  273. @Initial revision
  274. @
  275. text
  276. @d28 4
  277. d34 1
  278. d37 6
  279. d52 1
  280. a52 1
  281.     while ((ch = getopt(argc, argv, "fs")) != EOF)
  282. d60 3
  283. d71 12
  284. d148 32
  285. d182 1
  286. a182 1
  287.     fputs("usage:\tln [-s] targetname [sourcename]\n\tln [-s] targetname1 targetname2 [... targetnameN] sourcedirectory\n", stderr);
  288. @
  289.